<?php
namespace app\admin\controller;
use app\model\Recharge as RechargeModel;
use app\model\User as UserModel;
use think\Db;


class Recharge extends Base
{
    public function __construct(){
        $model = new RechargeModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '充值';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function index(){
        $ulogin = $this->login_user;
        $model = $this->model;
        $map_params = [
            ['key'=>'recharge_sn','type'=>'like'],
            ['key'=>'status','type'=>'='],
            ['key'=>'user_id','type'=>'='],
        ];
        $map = getSearchParam($map_params);
        if (isSearchParam('start_time')) {
            $map[] = ['ctime','>=',strtotime(input('start_time'))];
        }
        if (isSearchParam('end_time')) {
            $map[] = ['ctime','<',strtotime(input('end_time'))];
        }

        $users = Db::name('user')->field('id,name')->select();
        $users  = arrayParamExtract($users ,'id','name');
        $search_params = [
            [
                ['key'=>'recharge_sn','name'=>'充值订单号','type'=>'text'],
                ['key'=>'status','name'=>'支付状态','type'=>'select1_1','params'=>$model->getStatusList()],
                ['key'=>'user_id','name'=>'用户','type'=>'select2_1','params'=>$users],
            ],[
                ['key'=>'start_time','name'=>'开始时间','type'=>'date'],
                ['key'=>'end_time','name'=>'结束时间','type'=>'date']
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);

        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',20)
        ];
        $list = $model->where($map)->order('id','desc')->paginate($page_param);
        $page = $list->render();

        $list = $list->toarray();
        foreach ($list['data'] as $k => $vo) {
            $list['data'][$k]['user'] = UserModel::cchRelation(['id'=>$vo['user_id']]);
        }
        return view('index', [
            'list'=>$list,
            'page'=>$page
        ]);
    }


    public function cancel($id){
        $model = $this->model;
        $data = $model->where('id',$id)->find();
        if ( $data['status'] != 0 ) {
            $this->error('订单状态不符');
        }
        if ( $model->where([
            ['id','=',$data['id']],
            ['status','=',0]
        ])->update(['status'=>-1,'utime'=>time()]) ) {
            $this->success('取消成功');
        }else{
            $this->error('取消失败');
        }
    }

    public function view($id){
        $model = $this->model;
        $data = $model->where('id',$id)->find();
        
        $alipaySevice = new \AlipayTradeService(config('config.alipay'));
        $RequestBuilder = new \AlipayTradeQueryContentBuilder();
        $RequestBuilder->setOutTradeNo($data['recharge_sn']);
        $response = $alipaySevice->Query($RequestBuilder);
        dump($response);
    }

    
    

   
}
